package com.nuanshui.heatedloan.web;

import com.nuanshui.heatedloan.entity.cms.security.Module;
import com.nuanshui.heatedloan.service.cms.security.ModuleService;
import com.nuanshui.heatedloan.util.dwz.DwzPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 模块管理的控制器
 */
@Controller
@RequestMapping("/cms/manage/module")
public class ApiModuleController extends ApiController{
	@Autowired
	private ModuleService moduleService;


	/**
	 * 保存模块
	 * 
	 * @return
	 */
//	@RequiresPermissions("Module:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String create(final Module module, final HttpServletRequest request,Long parentId) {
		Module parent = new Module();
		parent.setId(  parentId);
		module.setParent(  parent);
		moduleService.save(module);
		return success();
	}



	/**
	 * 修改模块
	 * 
	 * @return
	 */
//	@RequiresPermissions("Module:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String update(final Module module, Long parentId) {
		Module parent = new Module();
		parent.setId(  parentId);
		module.setParent(  parent);
		moduleService.save(module);
		return success();
	}

	/**
	 * 删除模块
	 * 
	 * @return
	 */
//	@RequiresPermissions("Module:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String delete( final Long id) {
			moduleService.delete(id);
		return success();
	}

	/**
	 * 跳转到模块树列表
	 */
	@RequiresPermissions("Module:view")
	@RequestMapping(value = "/tree/list", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String list(final Map<String, Object> map) {
		  List<Module> module = moduleService.getApiTree();
		map.put("list", module.get(0));
		return success( map );
	}

	/**
	 * 根据parentId得到下面的子模块列表
	 */
//	@RequiresPermissions("Module:view")
	@RequestMapping(value = "/list", method = { 
			RequestMethod.POST }, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String list(final DwzPage page,  final Long parentId,
			final String keywords,
			final HttpServletRequest request) {
		if(parentId == null) return success();
		List<Module> modules = null;
		if (StringUtils.isNotBlank(keywords)) {
			modules = moduleService.find(parentId, keywords, page);
		} else {
			modules = moduleService.find(parentId, page);
		}

		if(parentId!=null) {
			request.getSession().setAttribute("parentModule",
					moduleService.get(parentId));
		}
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("total", page.getTotalCount());
		map.put("list", modules);
//		map.put("keywords", keywords);

		return success( map );
	}

}
